package com.example.demo.controller;


import com.example.demo.enity.Bill;
import com.example.demo.enity.Provider;
import com.example.demo.enity.User;
import com.example.demo.service.BillListService;
import com.example.demo.service.ProviderListService;
import com.example.demo.util.PageInfoTool;
import com.example.demo.util.Tools;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class BillListOperationController {

    @Autowired
    BillListService billListService;

    @Autowired
    ProviderListService providerListService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/billList")
    public String billList(Bill bill, Model model, Integer pageNum) {

        Tools.billListMethod(model, providerListService, bill);

        PageInfoTool.pageInfoConfig(pageNum, model, billListService, bill);

        return "billList";
    }


    @GetMapping("/tobillAdd")
    public String tobillAdd(Model model) {

        String username = LoginExitController.username;
        model.addAttribute("username", username);

        List<Provider> providerList = providerListService.getList(null);
        model.addAttribute("providerList", providerList);

        return "billAdd";
    }


//    @GetMapping("/getProviderList")
//    @ResponseBody
//    public List<Provider> getProviderList() {
//        List<Provider> providerList = providerListService.findByWhere(null);
//        return providerList;
//    }


    //根据表单的提交请求地址 必须一致 否则接受不到参数
    @PostMapping("/billAdd")
    public String billAdd(Bill bill, Model model) {

        String success="billList";
        String fail="billAdd";

        String supplierId = bill.getSupplier();
        try {
            Integer supplierCode = Integer.valueOf(supplierId);
            Provider provider = providerListService.findById(supplierCode);
            bill.setSupplier(provider.getSupplierName());
            bill.setPId(provider.getCode());
            bill.setCreateTime(new Date());
        } catch (Exception e) {
            model.addAttribute("error", "请选择供应商！");

            List<Provider> providerList = providerListService.getList(null);
            model.addAttribute("providerList", providerList);

            return "billAdd";
        }

        String result = Tools.addObject(bill, model, billListService, success, fail);

        return result;
    }


    @GetMapping("/billView")
    public String operation(Model model, Integer billId) {

        String username = LoginExitController.username;
        model.addAttribute("username", username);

        Bill bill = billListService.findById(billId);
        model.addAttribute("bill", bill);

        return "billView";
    }

    @GetMapping("/billUpdate")
    public String billUpdate(Model model, Integer billId) {
        Bill bill = billListService.findById(billId);
        model.addAttribute("bill", bill);

        String supplier = bill.getSupplier();
        model.addAttribute("supplier", supplier);

        List<Provider> providerList = providerListService.getList(null);
        model.addAttribute("providerList", providerList);

        return "billUpdate";
    }


    @GetMapping("/update")
    public String update(Bill bill, Model model) {

        String success="billList";
        String fail="billUpdate";

        String supplierId = bill.getSupplier();
        try {
            Integer supplierCode = Integer.valueOf(supplierId);
            Provider provider = providerListService.findById(supplierCode);
            bill.setSupplier(provider.getSupplierName());
            bill.setPId(provider.getCode());
        } catch (Exception e) {
            model.addAttribute("error", "请选择供应商！");

            List<Provider> providerList = providerListService.getList(null);
            model.addAttribute("providerList", providerList);

            return "billUpdate";
        }

        String result = Tools.updateObject(bill, model, billListService, success, fail);
        return result;

    }


    @GetMapping("/remove")
    public String remove(Integer billId, Model model) {
        try {
            billListService.remove(billId);
        } catch (Exception e) {
            String message = "删除失败";
            model.addAttribute("message", message);
            return "billList";
        }
        return "redirect:/billList";
    }

}
